//
//  StrategyHomeViewController.m
//  WisdomZhuHai
//
//  Created by Admin on 14-10-28.
//  Copyright (c) 2014年 wyd. All rights reserved.
//

#import "StrategyHomeViewController.h"
#import "ProtocolManager.h"
#import "Common.h"
#import "StrategySubViewController.h"
//玩法布局控制类

@interface StrategyHomeViewController ()

@property (nonatomic,strong) NSMutableArray* strategyViews;

@property (nonatomic,strong) NSArray* classifyList;

@end

@implementation StrategyHomeViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupStrategyViews];
    
    [self getClassify];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)onTapStrategyView:(UITapGestureRecognizer*)gesture
{
    NSLog(@"onTapStrategyView %d", gesture.view.tag);
    StrategySubViewController *strategySubViewCtrl = [[StrategySubViewController alloc] initWithFrame:[Common shareInstance].contentFrameNoTabBar];
    strategySubViewCtrl.classifyId = gesture.view.tag;
    strategySubViewCtrl.hidesBottomBarWhenPushed = YES;
    [self.navigationCtrl pushViewController:strategySubViewCtrl animated:YES];
}

#pragma mark - Private
- (void)setupStrategyViews
{
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake( 8, 8, self.view.bounds.size.width - 16, self.view.bounds.size.height)];
    scrollView.alwaysBounceVertical = YES;
    [self.view addSubview:scrollView];
    self.strategyViews = [NSMutableArray array];
    
    CGFloat width = scrollView.bounds.size.width;//视图组总宽
    CGFloat interval = 5.0;//各矩形间距
    CGFloat halfWidth = (width - interval)/2;//半个视图组总宽
    CGFloat quarterWidth = (width - halfWidth - interval)/2;//小矩形宽
    CGFloat rowHeight = halfWidth*0.58;//竖长矩形宽
    CGRect rects[] = {
//        CGRectMake(0, 0, width, rowHeight*2+interval),
////        CGRectMake(halfWidth+interval, 0, halfWidth, rowHeight),//Bable-Note
//        CGRectMake(halfWidth+interval, 0, halfWidth/2, rowHeight),
//        CGRectMake(halfWidth+interval*2+halfWidth/2, 0, halfWidth/2, rowHeight),
//        CGRectMake(halfWidth+interval, rowHeight+interval, halfWidth, rowHeight),
//        
//        CGRectMake(0, (rowHeight+interval)*2, quarterWidth, rowHeight),
//        CGRectMake(quarterWidth+interval, (rowHeight+interval)*2, halfWidth, rowHeight),
//        CGRectMake(quarterWidth+halfWidth+interval*2, (rowHeight+interval)*2, quarterWidth, rowHeight),
//        
//        CGRectMake(0, (rowHeight+interval)*3, width-interval-rowHeight, rowHeight),
//        CGRectMake(0, (rowHeight+interval)*4, width-interval-rowHeight, rowHeight),
//        CGRectMake(width-rowHeight, (rowHeight+interval)*3, rowHeight, rowHeight*2+interval),
                CGRectMake(0, 0, width, rowHeight*2+interval),
                CGRectMake(0, rowHeight*2+interval, width, rowHeight*2+interval),
                CGRectMake(0, 2*(rowHeight*2+interval), width, rowHeight*2+interval),
                CGRectMake(0, 3*(rowHeight*2+interval), width, rowHeight*2+interval),
                CGRectMake(0, 4*(rowHeight*2+interval), width, rowHeight*2+interval),
                CGRectMake(0, 5*(rowHeight*2+interval), width, rowHeight*2+interval),
                CGRectMake(0, 6*(rowHeight*2+interval), width, rowHeight*2+interval),
                CGRectMake(0, 7*(rowHeight*2+interval), width, rowHeight*2+interval),
   CGRectMake(0, 8*(rowHeight*2+interval), width, rowHeight*2+interval),
  CGRectMake(0, 9*(rowHeight*2+interval), width, rowHeight*2+interval),
    };
    
    CGFloat height = (rowHeight+interval)*5;
    scrollView.contentSize = CGSizeMake(0, height);
    
    for (int i=0; i<sizeof(rects)/sizeof(CGRect)+1; i++) {
//    for (int i=0; i<sizeof(rects); i++) {//Bable-Noted
        UIView *view = [self createStrategyView:rects[i]];
        [self.strategyViews addObject:view];
        [scrollView addSubview:view];
    }
}

- (UIView*)createStrategyView:(CGRect)rect
{
    UIView *view = [[UIView alloc] initWithFrame:rect];
    view.backgroundColor = [UIColor clearColor];
    
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapStrategyView:)];
    [view addGestureRecognizer:tapGesture];
    
    UIImageView* imgView = [[UIImageView alloc] initWithFrame:view.bounds];
    imgView.tag = 1000;
    imgView.contentMode = UIViewContentModeScaleAspectFill;
    imgView.clipsToBounds = YES;
    imgView.image = [Common getPlaceholderImage:ePIT_Strategy];
    [view addSubview:imgView];
    
    return view;
}

- (void)updateStrategyView:(int)index
{
    if (index >= self.classifyList.count || index >= self.strategyViews.count) {
        return;
    }
    NSDictionary* dic = self.classifyList[index];
    int classifyId = [[Common valueForKey:P_ID dic:dic] intValue];
    NSString * imgUrl = [Common valueForKey:P_ALBUM dic:dic];//获取图片
    
    UIView *view = self.strategyViews[index];
    view.tag = classifyId;
    UIImageView *imgView = (UIImageView*)[view viewWithTag:1000];
    [imgView setImageWithURL:[Common getFullDownloadPathByRelativePath:imgUrl] placeholderImage:[Common getPlaceholderImage:ePIT_Strategy]];
}

#pragma mark - Protocol request
- (void)getClassify
{
    [[ProtocolManager shareInstance] getClassify:self selector:@selector(getClassifyCallback:)];
}

#pragma mark - Protocol call back
- (void)getClassifyCallback:(NSDictionary*)dic
{
    NSLog(@"getClassifyCallback %@", dic);
    if (dic == nil) {
        return;
    }
    id list = [Common valueForKey:P_LIST dic:dic];
    if (![list isKindOfClass:[NSArray class]]) {
        return;
    }
    
    self.classifyList = list;
    for (int i=0; i<self.classifyList.count; i++) {
        [self updateStrategyView:i];
    }
}

@end
